$(function(){
	frame.secSelect($('#secIdInput'),{width:"140px",onBeforeLoad:beforeSecGridQuery});
	frame.secSelect($('#secIdSeach'));
	$('#dlg').dialog("close");
	$("#dg").edatagrid({
		url:'./query.json'
	});
	function beforeSecGridQuery(param){
		param.billType= $("#billType").combobox("getValue");
	}
});

function click_search() {
	var param = $("#search_param").form("getData");
	$('#dg').datagrid('load',param);
}

function secNameFormatter(value, row, index) {
	return frame.flex.getName("SMDC_SECURITY_SEL", row.secId);
}

function click_row_edit(){
	var rowData = $('#dg').datagrid("getSelected");
	if(!rowData){
		return;
	}
	openBillDlg(false);
	edittype="beginedit";
	$('#ff').form("load",rowData);
}

function click_add(){
	edittype="add";
	openBillDlg(true);
	var billTime = (new Date()).getTime();
	$('#ff').form("load",{billTime:billTime});
}

function openBillDlg(feeAuto){
	$('#ff').form("clear");
	$('#dlg').dialog("open");
	feeAutoChange(feeAuto)
}

function click_delete(){
	$.messager.confirm('确认','您确认想要删除吗？',
			function(r){    
			    if (r){
			    	delete_selected_data();
			    }
			}
		);
}

function delete_selected_data(){
	var billIds="";
	var selData = $("#dg").edatagrid("getSelections");
	for(var i=0;i<selData.length;i++){
		billIds += ","+selData[i].billId;
	}
	if(!billIds.length>1){
		return;
	}
	$.messager.confirm('确认', '您确认想要删除吗？',
		function(r) {
			if (r) {
				$.post("./delete.json",
					{billIds : billIds},
					function(data) {
						if (data.sucesse == true) {
							$('#dg').edatagrid("clearSelections");
							$('#dg').edatagrid("reload");
						}
					}
				);
			}
		});
}

function dlg_click_save(){
	$('#ff').form('ajaxSubmit',{
			data:{autoFee:$("#feeAutoButton").switchbutton("options").checked},
			url: "./save.json",
			success: function(data){
				if(data.sucesse==true){
					dlg_click_cancle();
					$('#dg').edatagrid("reload");
				}
			} 
		});
}

function dlg_click_delete() {
	$.messager.confirm('确认', '您确认想要删除吗？',
		function(r) {
			if (r) {
				$('#ff').form('ajaxSubmit', {
					url : "./delete.json",
					success : function(data) {
						if (data.sucesse == true) {
							dlg_click_cancle();
							$('#dg').edatagrid("reload");
						}
					}
				});
			}
		}
	);
}

function dlg_click_cancle(){
	$('#dlg').dialog("close");
}

function click_open_upload(){
	$.upload({
		action : frame.fullUrl('/trade/bill/upload.json'), //上传地址
		fileName : "file",    //文件名称。用于后台接收
		accept : ".xlsx,.xls,.csv" //文件类型
	});
}

function feeAutoChange(check){
	if(check){
		$("#feeAutoButton").switchbutton("check");
		$("#feeCommision").numberbox("disable");
		$("#feeTax").numberbox("disable");
		$("#feeOther").numberbox("disable");
	} else{
		$("#feeAutoButton").switchbutton("uncheck");
		$("#feeCommision").numberbox("enable");
		$("#feeTax").numberbox("enable");
		$("#feeOther").numberbox("enable");
	}
}

function profitStyle(value,row,index){
	if (value > 0){
		return 'color:red;';
	} else if (value < 0){
		return 'color:green;';
	}
}

function billTypeChange(newValue,oldValue){
	if(newValue == "transfer" || newValue == "oci"){
		$("#secIdInput").combogrid("disable");
		$("#secIdInput").combogrid("clear");
		$("#billTradeMoney").numberbox("enable");
		$("#secNum").numberbox("disable");
		$("#secNum").numberbox("clear");
		$("#secPrice").numberbox("disable");
		$("#secPrice").numberbox("clear");
	} else if (newValue.indexOf("div")>-1){
		$("#secIdInput").combogrid('grid').datagrid("reload");
		$("#secIdInput").combogrid("enable");
		$("#billTradeMoney").numberbox("enable");
		$("#secNum").numberbox("enable");
		$("#secPrice").numberbox("disable");
		$("#secPrice").numberbox("clear");
	} else {
		$("#secIdInput").combogrid('grid').datagrid("reload");
		$("#secIdInput").combogrid("enable");
		$("#billTradeMoney").numberbox("clear");
		$("#billTradeMoney").numberbox("disable");
		$("#secNum").numberbox("enable");
		$("#secPrice").numberbox("enable");
	}
}

